package service

import (
	"ginblog/model"
	errMsg "ginblog/model/err"
	"gorm.io/gorm"
)

type Article interface {
}
type ArticleRepo struct {
	db *gorm.DB
}

func NewArticleRepo(db *gorm.DB) *ArticleRepo {
	return &ArticleRepo{db: db}
}
func GetArticle(id int) (*model.Article, errMsg.Code) {
	var (
		data = model.Article{
			Model: gorm.Model{
				ID: uint(id),
			},
		}
	)
	err := data.Get()
	r := NewReturn()
	r.Check(err != nil && err != gorm.ErrRecordNotFound, errMsg.DB_ERROR)
	r.Check(err == gorm.ErrRecordNotFound, errMsg.ERROR_Article_NOT_EXIST)
	return &data, r.C
}

func AddArticle(data *model.Article) errMsg.Code {
	n, err := data.Create()
	r := NewReturn()
	r.Check(err != nil, errMsg.DB_ERROR)
	r.Check(n < 1, errMsg.DB_OPTION_FAIL)
	return r.C
}
func DeleteArticle(data *model.Article) errMsg.Code {
	n, err := data.Delete()
	r := NewReturn()
	r.Check(err != nil, errMsg.DB_ERROR)
	r.Check(n < 1, errMsg.DB_OPTION_FAIL)
	return r.C
}
func UpdateArticle(cate *model.Article, data map[string]interface{}) errMsg.Code {
	n, err := cate.Update(data)
	r := NewReturn()
	r.Check(err != nil, errMsg.DB_ERROR)
	r.Check(n < 1, errMsg.DB_OPTION_FAIL)
	return r.C
}
func GetCateArticleList() {

}
